<template>
  <div class="home">
    <div class="hero-section">
      <div class="hero-content">
        <h1 class="hero-title">🎰 欢迎来到转盘抽奖系统</h1>
        <p class="hero-subtitle">体验刺激的抽奖乐趣，赢取丰厚奖品！</p>
        <div class="hero-actions">
          <el-button 
            v-if="!userStore.isLoggedIn" 
            type="primary" 
            size="large" 
            @click="$router.push('/login')"
          >
            立即登录
          </el-button>
          <el-button 
            v-else 
            type="primary" 
            size="large" 
            @click="$router.push('/lottery')"
          >
            开始抽奖
          </el-button>
          <el-button 
            v-if="!userStore.isLoggedIn" 
            size="large" 
            @click="$router.push('/register')"
          >
            免费注册
          </el-button>
        </div>
      </div>
    </div>

    <div class="features-section">
      <div class="container">
        <h2 class="section-title">系统特色</h2>
        <div class="features-grid">
          <div class="feature-card">
            <div class="feature-icon">🎯</div>
            <h3>公平抽奖</h3>
            <p>采用先进的随机算法，确保每次抽奖都公平公正</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">🏆</div>
            <h3>丰富奖品</h3>
            <p>多种奖品等级，从数码产品到生活用品应有尽有</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">📱</div>
            <h3>操作简单</h3>
            <p>界面简洁直观，一键即可参与抽奖活动</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">🔒</div>
            <h3>安全可靠</h3>
            <p>采用JWT认证，保障用户信息和抽奖记录安全</p>
          </div>
        </div>
      </div>
    </div>

    <div class="stats-section" v-if="stats">
      <div class="container">
        <h2 class="section-title">系统统计</h2>
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-number">{{ stats.totalUsers }}</div>
            <div class="stat-label">注册用户</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ stats.totalLotteries }}</div>
            <div class="stat-label">总抽奖次数</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ stats.totalWinnings }}</div>
            <div class="stat-label">中奖次数</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ stats.winRate }}%</div>
            <div class="stat-label">中奖率</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '../stores/user'
import axios from 'axios'

const userStore = useUserStore()
const stats = ref(null)

const fetchStats = async () => {
  try {
    // 如果用户是管理员，获取统计数据
    if (userStore.isAdmin) {
      const response = await axios.get('/api/admin/statistics')
      stats.value = response.data
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

onMounted(() => {
  fetchStats()
})
</script>

<style scoped>
.home {
  min-height: 100vh;
}

.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 100px 0;
  text-align: center;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
}

.hero-title {
  font-size: 3.5rem;
  margin-bottom: 20px;
  font-weight: 700;
}

.hero-subtitle {
  font-size: 1.5rem;
  margin-bottom: 40px;
  opacity: 0.9;
}

.hero-actions {
  display: flex;
  gap: 20px;
  justify-content: center;
  flex-wrap: wrap;
}

.features-section {
  padding: 80px 0;
  background-color: white;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-title {
  text-align: center;
  font-size: 2.5rem;
  margin-bottom: 60px;
  color: #2c3e50;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 40px;
}

.feature-card {
  text-align: center;
  padding: 40px 20px;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-icon {
  font-size: 3rem;
  margin-bottom: 20px;
}

.feature-card h3 {
  font-size: 1.5rem;
  margin-bottom: 15px;
  color: #2c3e50;
}

.feature-card p {
  color: #7f8c8d;
  line-height: 1.6;
}

.stats-section {
  padding: 80px 0;
  background-color: #f8f9fa;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 30px;
}

.stat-card {
  background: white;
  padding: 40px 20px;
  border-radius: 10px;
  text-align: center;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.stat-number {
  font-size: 3rem;
  font-weight: 700;
  color: #667eea;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 1.1rem;
  color: #7f8c8d;
}

@media (max-width: 768px) {
  .hero-title {
    font-size: 2.5rem;
  }
  
  .hero-subtitle {
    font-size: 1.2rem;
  }
  
  .hero-actions {
    flex-direction: column;
    align-items: center;
  }
  
  .section-title {
    font-size: 2rem;
  }
}
</style>